from flask import Blueprint, jsonify, request
from flask_jwt_extended import jwt_required
from libs.conn import *
from libs.utils import *

apps_bp = Blueprint('apps', __name__)

# app注册
@apps_bp.route('/api/app_register', methods=['POST'])
def app_register():
    password = request.json.get('password')
    app_id = request.json.get('app_id')
    app_name = request.json.get('app_name')
    app_secret = request.json.get('app_secret')
    my_password = get_config('relay_service_password')
    if get_config('relay_service_enable') != '1': # 如果未开启中继服务
        return { 'status': 'error', 'data': '未开启中继服务' }
    print('relay_register_enable', get_config('relay_register_enable'))
    if get_config('relay_register_enable') != '1': # 如果未开启app注册
        return { 'status': 'error', 'data': '未开启app注册' }
    if password != my_password:
        return { 'status': 'error', 'data': '密码错误' }
    row = fetchone("SELECT * FROM apps WHERE app_id = ?", (app_id,))
    if is_string_valid(app_id) and is_string_valid(app_name) and is_string_valid(app_secret):
        if row is None:
            execute("INSERT INTO apps (app_id, app_name, app_secret) VALUES (?, ?, ?)", (app_id, app_name, app_secret))
            return { 'status': 'ok', 'data': 'success' }
        else:
            # app已存在，更新密钥和app_name
            execute("UPDATE apps SET app_name = ?, app_secret = ? WHERE app_id = ?", (app_name, app_secret, app_id))
            return { 'status': 'ok', 'data': '已更新app密钥和app名称' }

    return { 'status': 'error', 'data': '参数错误' }

@apps_bp.route('/api/add_app', methods=['POST'])
@jwt_required()
def add_app():
    app_id = request.json.get('app_id')
    app_name = request.json.get('app_name')
    app_secret = request.json.get('app_secret')
    row = fetchone("SELECT * FROM apps WHERE app_id = ?", (app_id,))
    if row is None:
        execute("INSERT INTO apps (app_id, app_name, app_secret, app_checked) VALUES (?, ?, ?, ?)", (app_id, app_name, app_secret, 1))
        return { 'status': 'ok', 'data': 'success' }
    else:
        return { 'status': 'error', 'data': 'app已存在' }
    
@apps_bp.route('/api/update_app', methods=['POST'])
@jwt_required()
def update_app():
    id = int(request.json.get('id'))
    app_id = request.json.get('app_id')
    app_name = request.json.get('app_name')
    app_secret = request.json.get('app_secret')
    execute("UPDATE apps SET app_id = ?, app_name = ?, app_secret = ? WHERE id = ?", (app_id, app_name, app_secret, id))
    return { 'status': 'ok', 'data': 'success' }

@apps_bp.route('/api/get_apps', methods=['GET'])
@jwt_required()
def get_apps():
    rows = fetchall("SELECT * FROM apps")
    return { 'status': 'ok', 'data': rows }

# app检测通过或不通过
@apps_bp.route('/api/app_check', methods=['POST'])
@jwt_required()
def app_check():
    app_id = request.json.get('app_id')
    app_check = int(request.json.get('app_check'))
    the_app = fetchone("SELECT app_checked FROM apps WHERE app_id = ?", (app_id,))
    if the_app is None:
        return { 'status': 'error', 'data': 'app不存在' }
    if app_check == 1:
        if the_app['app_checked'] == 1:
            return { 'status': 'error', 'data': 'app已审核' }
        execute("UPDATE apps SET app_checked = 1 WHERE app_id = ?", (app_id,))
        return { 'status': 'ok', 'data': 'success' }
    else:
        # 如果审核不通过，则删除该app
        execute("DELETE FROM apps WHERE app_id = ?", (app_id,))
        return { 'status': 'ok', 'data': 'success' }

# app enable
@apps_bp.route('/api/app_enable', methods=['POST'])
@jwt_required()
def app_enable():
    app_id = request.json.get('app_id')
    app_enable = int(request.json.get('app_enable'))
    execute("UPDATE apps SET app_enable = ? WHERE app_id = ?", (app_enable, app_id))
    return { 'status': 'ok', 'data': 'success' }

# app删除
@apps_bp.route('/api/delete_app', methods=['POST'])
@jwt_required()
def app_delete():
    app_id = request.json.get('app_id')
    execute("DELETE FROM apps WHERE app_id = ?", (app_id,))
    return { 'status': 'ok', 'data': 'success' }
